﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.BackgroundTasks;
using RMS.Utils.EventAggregators;

namespace RMS.Utils.RulesManager
{
    public class RulesExecuter : IBackgroundTask
    {
        private IEventAggregator _eventAggregator;
        private IRulesManager _rulesManager;

        public RulesExecuter(IEventAggregator eventAggregator, IRulesManager rulesManager)
        {
            this._eventAggregator = eventAggregator;
            this._rulesManager = rulesManager;
        }


        public void Start()
        {
            _eventAggregator.GetEvent<EntityAction>().Subscribe(OnAction);
        }

        private void OnAction(EntityActionEventArgs e)
        {
            var dlgt = _rulesManager.Find(e.Entity.GetType(), e.State);
            if (dlgt != null)
                dlgt.DynamicInvoke(e.Entity);
        }

        public void Stop()
        {
            _eventAggregator.GetEvent<EntityAction>().Unsubscribe(OnAction);
        }
    }
}
